package com.myk.game.lib.base;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.myk.game.lib.utils.FastUtil;

/**
 * 页面容器布局
 */
public class PageContainer extends FrameLayout {
    public PageContainer(@NonNull Context context) {
        super(context);
    }

    public PageContainer(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 移除其它所有Page并添加传入的page
     */
    public void setPage(AbsBasePage<?> page) {
        if (getChildCount() > 0) {
            if (getChildAt(0) != page) {
                removeAllViews();
                addView(page);
            }
        }
        else {
            addView(page);
        }
    }

    /**
     * 处理返回事件
     */
    public boolean onBackPressed() {
        if (getChildCount() > 0) {
            AbsBasePage<?> page = (AbsBasePage<?>) getChildAt(0);
            if (page.onBackPressedCallback != null) {
                page.onBackPressedCallback.run();
                return true;
            }
        }
        return false;
    }

}
